#!/usr/bin/env bash
export MASTER_IPS='H1=k8s-master-01=192.168.100.16 H2=k8s-master-02=192.168.100.17 H3=k8s-master-03=192.168.100.18 V1=k8s-master-vip-01=192.168.100.3'
export ETCDHOST0=172.16.100.51
export ETCDHOST1=172.16.100.52
export ETCDHOST2=172.16.100.53
# 网卡名称
export ETH_INT=ens192
# k8s化版本号
export K8SVERSION=v1.13.5
export VIP_PORT=8443
i=0
for IP in $MASTER_IPS
do
i=$(($i+1))
export $IP
HOST=`echo $IP |awk -F "=" '{print $1}'`
IPS=`eval echo '$'$HOST`
hostname=`echo $IPS |awk -F "=" '{print $1}'`
hostip=`echo $IPS |awk -F "=" '{print $2}'`
if [[ "$i" -ge "6" ]];then
  export VIP=$hostip
  export HOSTVIPNAME=$hostname
  sed -i '$a\'$VIP' '$HOSTVIPNAME'' /etc/hosts
  else
  export HOST$i=$hostip
fi
sed -i '$a\'$hostip' '$hostname'' /etc/hosts
done
hostname=`hostname`
hostip=`ip a |grep ${ETH_INT} |awk '{print $2}' |sed -n '2p' |awk -F "/" '{print $1}'`
sed -i '$a\'$hostip' '$hostname'' /etc/hosts
host=`hostname`
if [[ "$host" != "k8s-master-01" ]];then
    exit 1
    else
        echo `hostname`
fi
cat <<EOF > /root/kubeadm-config-init.yaml
apiVersion: kubeadm.k8s.io/v1beta1
bootstrapTokens:
- groups:
  - system:bootstrappers:kubeadm:default-node-token
  token: abcdef.0123456789abcdef
  ttl: 0s
  usages:
  - signing
  - authentication
kind: InitConfiguration
---
apiServer:
  certSANs:
  - "k8s-master-01"
  - "k8s-master-02"
  - "k8s-master-03"
  - "k8s-master-04"
  - "k8s-master-05"
  - "k8s-master-06"
  - "k8s-master-07"
  - "k8s-master-08"
  - "k8s-master-09"
  - "${HOSTVIPNAME}"
  - "127.0.0.1"
  - "$HOST1"
  - "$HOST2"
  - "$HOST3"
  - "$HOST4"
  - "$HOST5"
  - "$VIP"
  extraArgs:
    authorization-mode: Node,RBAC
    advertise-address: 0.0.0.0
    service-node-port-range: "10000-40000"
controlPlaneEndpoint: "${HOSTVIPNAME}:${VIP_PORT}"
controllerManager:
  extraArgs:
    address: 0.0.0.0
scheduler:
  extraArgs:
    address: 0.0.0.0
apiVersion: kubeadm.k8s.io/v1beta1
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
dns:
  type: CoreDNS
etcd:
  external:
    endpoints:
    - https://$ETCDHOST0:2379
    - https://$ETCDHOST1:2379
    - https://$ETCDHOST2:2379
    caFile: /etc/kubernetes/pki/etcd/ca.crt
    certFile: /etc/kubernetes/pki/apiserver-etcd-client.crt
    keyFile: /etc/kubernetes/pki/apiserver-etcd-client.key
imageRepository: registry.aliyuncs.com/google_containers
kind: ClusterConfiguration
kubernetesVersion: ${K8SVERSION}
networking:
  dnsDomain: cluster.local
  podSubnet: 10.244.0.0/16
  serviceSubnet: 10.96.0.0/12
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
mode: "ipvs"
EOF